package com.hkemmm.controller;

import com.hkemmm.bean.UserInfoBean;
import com.hkemmm.service.IUserService;
import com.hkemmm.utlis.Myutils;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserAjaxController {
    @Resource
    private IUserService userService;


    @ResponseBody
    @RequestMapping("/ajaxdemo")
    public String doAjaxDemo(String userName, String userAddress, HttpServletRequest request) {
        System.out.println("正在登陆:" + userName + "\t密码:" + userAddress);


        return "ok";
    }

    @ResponseBody
    @RequestMapping("/ajaxdemo2")
    public List<UserInfoBean> doAjaxDemo2(String userName, String userAddress, HttpServletRequest request) {
        List<UserInfoBean> list = userService.selectALL();

        List<UserInfoBean> lstBean = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            lstBean.add(list.get(i));
        }

        return lstBean;
    }


    @RequestMapping("/ajaxdemo3")
    public ResponseEntity<List<UserInfoBean>> doAjaxDemo3(String userName, String userAddress, HttpServletRequest request) {
        UserInfoBean user = userService.selectByPrimaryKey(1);
        List<UserInfoBean> lstBean = new ArrayList<>();

        lstBean.add(user);


        //return new ResponseEntity<>(lstBean,HttpStatus.OK);
        //return ResponseEntity.status(HttpStatus.OK).body(lstBean);
        return ResponseEntity.ok(lstBean);
    }

    @RequestMapping("/ajaxdemo4")
    public ResponseEntity<List<UserInfoBean>> doAjaxDemo4(UserInfoBean userInfoBean, Date userregdate2) {

        //System.out.println(userInfoBean + "----" + userregdate2);
        List<UserInfoBean> lstBean = userService.selectByPrimary(userInfoBean, userregdate2);
        //System.out.println(lstBean);

        return ResponseEntity.ok(lstBean);
    }

    @ResponseBody
    @RequestMapping("/ajaxinsert")
    public String insertUser(UserInfoBean userInfoBean,@RequestParam("file-userimg") MultipartFile multipartFile){
        Myutils.doSingleFileUpload(multipartFile,"uploadfiles", "userimg", userInfoBean, "userimg");
        System.out.println(userInfoBean);

        int i = 1;//userService.addAndUpdateUserInfo(userInfoBean);
        if (i > 0) {

            return "ok";
        } else {
            return "error";
        }
    }

    @ResponseBody
    @RequestMapping("/selectByKey")
    public UserInfoBean selectByPrimaryKey(UserInfoBean userInfoBean){
        UserInfoBean user = userService.selectByPrimaryKey(userInfoBean.getUserid());

        return user;
    }
}
